package com.atguigu.spzx.cloud.order.config;

import feign.RetryableException;
import feign.Retryer;
import org.springframework.stereotype.Component;
//重试机制
@Component
public class UserClientRetryer implements Retryer {
    private int retry = 3;
    private int count = 1;
    //如果此方法抛出异常 停止重试
    @Override
    public void continueOrPropagate(RetryableException e) {
        //我们希望第一次请求失败 再重试2次，feign一共提交了三次请求
        if(count>=retry){
            //停止重试时 重置count值
            count = 1;
            //抛出异常 停止重试
            throw new RuntimeException(e);
        }
        count++;
    }

    //openfeign会自动到容器中装配Retryer实例 调用它的clone()方法获取重试对象
    @Override
    public Retryer clone() {
        return new UserClientRetryer();
    }
}
